﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace TubeBurstAnalysis
{
    public partial class FormFilter : Form
    {
        /// <summary>
        /// 用户选择显示的阀门属性Map，key为Name，Value为中文名
        /// </summary>
        public Dictionary<string, string> shownPropMap;

        /// <summary>
        /// 阀门属性Map，key为Name，Value为中文名
        /// </summary>
        public Dictionary<string, string> propMap;

        /// <summary>
        /// 爆管分析窗体
        /// </summary>
        public FormAnalysis formResult;

        /// <summary>
        /// 属性显示对象列表
        /// </summary>
        private readonly List<IShownProp> propList = new List<IShownProp>();

        /// <summary>
        /// 展示的属性显示对象列表
        /// </summary>
        private readonly List<IShownProp> shownPropList = new List<IShownProp>();

        public FormFilter()
        {
            InitializeComponent();
        }

        internal void start()
        {
            shownPropMap = formResult.shownPropMap;
            propMap = formResult.propMap;
            generateList();
        }

        /// <summary>
        /// 根据propMap与shownPropMap显示listBox
        /// </summary>
        private void generateList()
        {
            propList.Clear();
            shownPropList.Clear();

            for (int i = 0; i < propMap.Count; i++)
            {
                KeyValuePair<string, string> item = propMap.ElementAt(i);//获取当前键值对
                IShownProp shownProp = new IShownProp
                {
                    Name = item.Value //设置显示属性的Name
                }; //生成显示属性
                propList.Add(shownProp);
            }
            listBox1.DisplayMember = "Name";
            BindingSource bs1 = new BindingSource
            {
                DataSource = propList
            };
            listBox1.DataSource = bs1;

            for (int i = 0; i < shownPropMap.Count; i++)
            {
                KeyValuePair<string, string> item = shownPropMap.ElementAt(i);//获取当前键值对
                IShownProp shownProp = new IShownProp
                {
                    Name = item.Value //设置显示属性的Name
                }; //生成显示属性
                shownPropList.Add(shownProp);
            }
            listBox2.DisplayMember = "Name";
            BindingSource bs2 = new BindingSource
            {
                DataSource = shownPropList
            };
            listBox2.DataSource = bs2;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            formResult.generateDataGridView();
            Close();
        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            //获取选择的属性名
            string propString = ((IShownProp)listBox1.SelectedItem).Name.ToString();
            //遍历寻找相关键值对
            foreach (KeyValuePair<string, string> entry in propMap)
            {
                //如果找到，添加到shownPropMap
                if (entry.Value.ToString() == propString)
                {
                    //找到后的操作
                    if (shownPropMap.ContainsKey(entry.Key))
                    {
                        MessageBox.Show("该属性已添加");
                    }
                    else
                    {
                        shownPropMap.Add(entry.Key, entry.Value);
                        //重新生成列表
                        generateList();
                    }
                    return;
                }
            }
        }

        private void listBox2_DoubleClick(object sender, EventArgs e)
        {
            //获取选择的属性名
            string propString = ((IShownProp)listBox2.SelectedItem).Name.ToString();
            //遍历寻找相关键值对
            foreach (KeyValuePair<string, string> entry in shownPropMap)
            {
                //如果找到，删除
                if (entry.Value == propString)
                {
                    //直接添加即可,如果有则覆盖
                    shownPropMap.Remove(entry.Key);
                    //重新生成列表
                    generateList();
                    return;
                }
            }

        }
    }
}
